package com.music.controller;

import com.music.pojo.UserInfo;
import com.music.service.UserService;
import com.music.utils.MailUtils;
import com.music.utils.QueryVo;
import com.music.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("query")
    public QueryVo comprehensiveQuery(String param){
        return userService.find_comprehensive(param);
    }

    @RequestMapping("login")
    public ResultInfo login(@RequestBody UserInfo user, HttpSession httpSession) throws ServletException, IOException {
        ResultInfo resultInfo = new ResultInfo();
        String email = user.getEmail();
        String password = user.getPassword();
        try {
            UserInfo userInfo = userService.find_login(email,password);
            httpSession.setAttribute("userInfo",userInfo);
            resultInfo.setFlag(true);
        }catch (Exception e){
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("用户名或密码错误");
        }
        return resultInfo;
    }

    @RequestMapping("register")
    public ResultInfo register(@RequestBody UserInfo userInfo){
        ResultInfo resultInfo = new ResultInfo();
        try {
            userInfo.setStatus(1);
            userService.add_register(userInfo);
            resultInfo.setFlag(true);
        }catch (Exception e){
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("注册失败");
        }
        return resultInfo;
    }

    //综合搜索
    @RequestMapping("search")
    public QueryVo find_comprehensive(String search){
        if (!search.equals("")){
            return userService.find_comprehensive(search);
        }else {
            return new QueryVo();
        }
    }

    @RequestMapping("session")
    public ResultInfo getSession(HttpSession httpSession){
        ResultInfo resultInfo = new ResultInfo();
        Object userInfo = httpSession.getAttribute("userInfo");
        if (userInfo == null){
            resultInfo.setFlag(false);
        }else {
            resultInfo.setFlag(true);
            resultInfo.setData(userInfo);
        }
        return resultInfo;
    }

    @RequestMapping("logout")
    public ResultInfo logout(HttpSession httpSession,HttpServletResponse response,HttpServletRequest request) throws IOException {
        httpSession.removeAttribute("userInfo");
        response.sendRedirect(request.getContextPath()+"/pages/index.html");
        return new ResultInfo(true);
    }

    //用户关注歌手
    @RequestMapping("follow")
    public ResultInfo follow(Integer sid,HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo==null){
            resultInfo = new ResultInfo(false);
        }else {
            userService.addUserSinger(sid,userInfo.getUid());
            resultInfo = new ResultInfo(true);
        }
        return resultInfo;
    }

    @RequestMapping("unfollow")
    public ResultInfo unFollow(Integer sid,HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        userService.removeUserSinger(sid,userInfo.getUid());
        ResultInfo resultInfo = new ResultInfo(true);
        return resultInfo;
    }

    @RequestMapping("findUserDetail")
    public ResultInfo findUserDetail(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo==null){
            resultInfo = new ResultInfo(false);
        }else {
            resultInfo = new ResultInfo(true);
            userInfo = userService.findUserMusicDetail(userInfo.getUid());
        }
        resultInfo.setData(userInfo);
        return resultInfo;
    }
}
